package com.whalehearing.tingyou.web.controller.content;

import java.util.List;
import java.util.Arrays;
import javax.servlet.http.HttpServletResponse;

import com.whalehearing.tingyou.domain.Article;
import com.whalehearing.tingyou.service.IArticleService;
import com.whalehearing.tingyou.common.annotation.DataSource;
import com.whalehearing.tingyou.common.enums.DataSourceType;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import com.whalehearing.tingyou.common.annotation.Log;
import com.whalehearing.tingyou.common.core.controller.BaseController;
import com.whalehearing.tingyou.common.core.domain.AjaxResult;
import com.whalehearing.tingyou.common.enums.BusinessType;
import com.whalehearing.tingyou.common.utils.poi.ExcelUtil;
import com.whalehearing.tingyou.common.core.page.TableDataInfo;

/**
 * 文章Controller
 *
 * @author zengjun-qxm
 * @date 2024-08-05
 */
@Api(tags="文章管理")
@RestController
@DataSource(DataSourceType.SLAVE)
@RequestMapping("system/content/article")
public class ArticleController extends BaseController
{
    @Autowired
    private IArticleService articleService;

    /**
     * 查询文章列表
     */
    @ApiOperation("查询文章列表")
    @PreAuthorize("@ss.hasPermi('content:article:list')")
    @GetMapping("/list")
    public TableDataInfo list(Article article)
    {
        startPage();
        List<Article> list = articleService.selectArticleList(article);
        return getDataTable(list);
    }

    /**
     * 查询文章列表-------------用于区分,若依前端调用,给移动端调用的
     */
    @ApiOperation("查询文章列表")
    @PostMapping("/getlist")
    public List<Article> getlist(@RequestBody @Validated Article article)
    {
        //只查询banner
//        article.setCategoryId(12L);
//        article.setState("已发布");
        List<Article> list = articleService.selectArticleList(article);
        return list;
    }

    /**
     * 导出文章列表
     */
    @ApiOperation("导出文章列表")
    @PreAuthorize("@ss.hasPermi('content:article:export')")
    @Log(title = "文章", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Article article)
    {
        List<Article> list = articleService.selectArticleList(article);
        ExcelUtil<Article> util = new ExcelUtil<Article>(Article.class);
        util.exportExcel(response, list, "文章数据");
    }

    /**
     * 获取文章详细信息
     */
    @ApiOperation("获取文章详细信息")
    @PreAuthorize("@ss.hasPermi('content:article:query')")
    @GetMapping(value = "/{articleId}")
    public AjaxResult getInfo(@PathVariable("articleId") Long articleId)
    {
        return success(articleService.getById(articleId));
    }

    /**
     * 新增文章
     */
    @ApiOperation("新增文章")
    @PreAuthorize("@ss.hasPermi('content:article:add')")
    @Log(title = "文章", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Article article)
    {
        return toAjax(articleService.save(article));
    }

    /**
     * 修改文章
     */
    @ApiOperation("修改文章")
    @PreAuthorize("@ss.hasPermi('content:article:edit')")
    @Log(title = "文章", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Article article)
    {
        return toAjax(articleService.updateById(article));
    }

    /**
     * 删除文章
     */
    @ApiOperation("删除文章")
    @PreAuthorize("@ss.hasPermi('content:article:remove')")
    @Log(title = "文章", businessType = BusinessType.DELETE)
	@DeleteMapping("/{articleIds}")
    public AjaxResult remove(@PathVariable Long[] articleIds)
    {
        return toAjax(articleService.removeByIds(Arrays.asList(articleIds)));
    }
}
